package com.kkyy.mymusic.controller;

import com.kkyy.common.utils.R;
import com.kkyy.mymusic.entity.MyMusicEntity;
import com.kkyy.mymusic.entity.NewMusicEntity;
import com.kkyy.mymusic.entity.UsersEntity;
import com.kkyy.mymusic.feign.FeignService;
import com.kkyy.mymusic.service.MyMusicService;
import com.kkyy.mymusic.service.UsersService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mymusic")
public class MyMusicController {
    @Autowired
    FeignService feignService;

    @Autowired
    MyMusicService myMusicService;

    @Autowired
    UsersService usersService;

    // 远程调用前端注册插入
    @RequestMapping("/insertUsers")
    void insertOne(@RequestParam("username") String username, @RequestParam("password") String password) {
        usersService.insertUsers(username, password);
    }

    // vue登录
    @RequestMapping("/vuelogin")
    public R vuelogin(@RequestParam("username") String username, @RequestParam("password") String password) {
        UsersEntity user = usersService.getUserByUsername(username);
        SimpleHash md5 = new SimpleHash("MD5", password, username);
        if (user == null) {
            return R.ok("用户不存在！");
        }
        if (md5.toString().equals(user.getPassword())) {
            return R.ok().put("username", user.getUsername());
        } else {
            return R.ok("账号或密码错误！");
        }
    }

    // 远程调用收藏删除(后台)
    @RequestMapping("/mymusicdel")
    void mymusicdel(@RequestParam("id") Integer id) {
        myMusicService.mymusicdel(id);
    }

    // vue注册
    @RequestMapping("/vueregist")
    @Transactional
    public R vueregist(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("fullname") String fullname, @RequestParam("mobile") String mobile) {
        UsersEntity user = usersService.getUserByUsername(username);
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        boolean result = mobile.matches(regex);
        if (user != null) {
            return R.ok("该用户已存在！");
        } else if (result) {
            SimpleHash pwd = new SimpleHash("MD5", password, username);
            usersService.insertUsers(username, pwd.toString());
            return R.ok("注册成功，请登录！");
        } else {
            return R.ok("请输入真实的手机号！");
        }
    }

    // 远程调用(删除前台用户)
    @RequestMapping("/usersdelete")
    void usersdelete(@RequestParam("id") Integer id) {
        myMusicService.usersdelete(id);
    }

    // 远程调用查看收藏(后台)
    @RequestMapping("/myCollection2")
    List<MyMusicEntity> myCollection2(@RequestParam("userId") Integer userId) {
        return myMusicService.myCollection(userId);
    }

    // 查看收藏(前端)
    @RequestMapping("/myCollection")
    R myCollection(@RequestParam("username") String username) {
        UsersEntity user = usersService.getUserByUsername(username);
        List<MyMusicEntity> myCollection = myMusicService.myCollection(user.getId());
        return R.ok().put("data", myCollection);
    }

    // 通过分类查看收藏(前端)
    @RequestMapping("/songsbytype")
    R songsbytype(@RequestParam("type") Integer type, @RequestParam("username") String username) {
        UsersEntity user = usersService.getUserByUsername(username);
        List<MyMusicEntity> myCollection = myMusicService.songsbytype(type, user.getId());
        return R.ok().put("data", myCollection);
    }

    // 添加收藏(前端)
    @RequestMapping("/save")
    public R save(@RequestParam("songsId") Integer songsId, @RequestParam("username") String username) {
        // 查到songs表对应的音乐
        NewMusicEntity newMusicEntity = feignService.getOneBySongsId(songsId);
        if (newMusicEntity != null) {
            MyMusicEntity myMusicEntity = new MyMusicEntity();
            myMusicEntity.setMymusicName(newMusicEntity.getSongsAlbumname());
            myMusicEntity.setMymusicArtistname(newMusicEntity.getSongsArtistname());
            myMusicEntity.setMymusicPicurl(newMusicEntity.getSongsAlbumpicurl());
            myMusicEntity.setMymusicLocationid(newMusicEntity.getSongsLocationid());
            myMusicEntity.setMymusicType(newMusicEntity.getSongsType());
            myMusicEntity.setMymusicDuration(newMusicEntity.getSongsDuration());
            myMusicEntity.setMymusicType(newMusicEntity.getSongsType());
            // 获取添加者的id
            UsersEntity user = usersService.getUserByUsername(username);
            myMusicEntity.setUserId(user.getId());
            myMusicService.insertOne(myMusicEntity);
            return R.ok("收藏成功！");
        } else {
            return R.ok("收藏失败！");
        }
    }
}